<?php

/**
 * CodeRS - Atviras kodas Lietuvai
 * Copyright (C) 2007 CodeRS www.coders.lt info@coders.lt
 * MightMedia TVS
 * blokai.php - Bloku administravimas
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 * 
 * Ši programa yra laisva. Jūs galite ją platinti ir/arba modifikuoti
 * remdamiesi Free Software Foundation paskelbtomis GNU Bendrosios
 * Viešosios licencijos sąlygomis: 2 licencijos versija, arba (savo
 * nuožiūra) bet kuria vėlesne versija.
 * 
 * Ši programa platinama su viltimi, kad ji bus naudinga, bet BE JOKIOS
 * GARANTIJOS; be jokios numanomos PERKAMUMO ar TINKAMUMO KONKRETIEMS
 * TIKSLAMS garantijos. Žiūrėkite GNU Bendrąją Viešąją licenciją norėdami
 * sužinoti smulkmenas.
 * 
 * Jūs turėjote kartu su šia programa gauti ir GNU Bendrosios Viešosios
 * licencijos kopija; jei ne - rašykite Free Software Foundation, Inc., 59
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**/

if ($_SERVER['PHP_SELF'] == __FILE__) { header('location: ?'); exit(); }
if (!ADMIN) { perkelti('?'); }

//uzkraunam apdorojimo klasę
require('tools/SLLists.class.php');
$sortableLists = new SLLists('scriptaculous');	// points to path of scriptaculous JS files

$listItemFormat = '<li id="item_%s"><strong>%s</strong> <a href="?id,46;a,9;r,%s" style="align:right">[r]</a> <a href="?id,46;a,9;d,%s" style="align:right" onClick="return confirm(\'Ar tikrai trinti panelę?\')">[d]</a></li>';  // two arguments are the idField and the displayField

$sortableLists->addList('kaire','paneles_kaire');
$sortableLists->addList('desine','paneles_desine');

if (isset($url['d']) && isnum($url['d']) && $url['d'] > 0) {
	$db->uzklausa("DELETE FROM `panel` WHERE `panel`.`id`= ".$db->fix((int)$url['d'])." LIMIT 1");
	perkelti("?id,46;a,9");
}

//naujo bloko sukurimas
elseif (isset($url['n']) && $url['n'] == 1) {
	if (isset($_POST['Nauja_panele']) && $_POST['Nauja_panele'] == "Sukurti") {
		$panel = input($_POST['Panel']);
		$file = input($_POST['File']);
		if (!file_exists("blokai/".$file)) { klaida("Klaida","<h3>Panelės failas <font color='red'>".basename($file)."</font> neegzistuoja.</h3>Atsiprašome patikrinkite ar įvesti duomenys teisingi. Jei taip įsitikinkite ar <b>blokai/</b> direktorijoje toks failas egzistuoja"); }
		else {
			if (empty($panel) || $panel =='') { $panel = basename($file, ".php"); }
			$align = input($_POST['Align']); if (strlen($align)>1) { $align = 'L'; }
			$show = input($_POST['Show']); if (strlen($show)>1) { $align = 'Y'; }
			$sql = "INSERT INTO `panel` (`panel`, `file`, `place`, `align`, `show`) VALUES (".$db->fix($panel).", ".$db->fix($file).", '0', ".$db->fix($align).", ".escape($show).")";
			$db->uzklausa($sql);
			perkelti("?id,46;a,9");
		}
	}
	foreach (glob("blokai/*.php") as $filename) {
		$sql = $db->uzklausa("SELECT panel FROM `panel` WHERE file=".$db->fix(basename($filename))." LIMIT 1");
		if ($db->masyvas($sql) == 0) {
			$paneles[basename($filename)] = basename($filename).": " . baitai(filesize($filename)) . "\n";
		}
	}
	if (!isset($paneles) || count($paneles) < 1) { klaida("Klaida","<h3>Nenaudojamų panelių nėra</h3>Norėdami į svetainę įdėti naują panelę, pasinaudodami ftp uploadinkite naują php failą į [blokai/] direktoriją"); }
	else {
		$panele = array(
			"Form"=>array("action"=>"","method"=>"post","enctype"=>"","id"=>"","class"=>"","name"=>"new_panel"),
			"Panelės avadinimas:"=>array("type"=>"text","value"=>"Nauja blokas","name"=>"Panel","style"=>"width:400px"),
			"Panelės failas:"=>array("type"=>"select","value"=>$paneles,"name"=>"File"),
			"Kurioje pusėje:"=>array("type"=>"select","value"=>array("L"=>"Kairė","R"=>"Dešinė"),"name"=>"Align"),
			"Ar rodyti panelę?"=>array("type"=>"select","value"=>array("Y"=>"Taip","N"=>"Ne"),"name"=>"Show"),
			""=>array("type"=>"submit","name"=>"Nauja_panele","value"=>"Sukurti")
		);
		
		require_once(HOME_DIR."variklis/class/forms_inputs.php");
		$bla = new forma();
		lentele("Naujas blokas",$bla->form($panele,"Naujo bloko sukūrimas"));
	}
}

//Paneles redagavimas
elseif (isset($url['r']) && isnum($url['r']) && $url['r'] > 0) {
	if (isset($_POST['Redaguoti_panele']) && $_POST['Redaguoti_panele'] == "Redaguoti") {
		$panel = input($_POST['Panel']);
		if (empty($panel) || $panel =='') { $panel = "Naujas blokas"; }
		$align = input($_POST['Align']); if (strlen($align)>1) { $align = 'L'; }
		$show = input($_POST['Show']); if (strlen($show)>1) { $align = 'Y'; }
		$sql = "UPDATE `panel` SET `panel`=".escape($panel).", `align`=".$db->fix($align).", `show`=".$db->fix($show)."  WHERE `id`=".$db->fix((int)$url['r']);
		$db->uzklausa($sql);
		perkelti("?id,46;a,9","header");
	}
	else {
		$sql = "SELECT * FROM `panel` WHERE `id`=".$db->fix((int)$url['r'])." LIMIT 1";
		$sql = $db->masyvas($db->uzklausa($sql));
		$panele = array(
			"Form"=>array("action"=>"","method"=>"post","enctype"=>"","id"=>"","class"=>"","name"=>"new_panel"),
			"Panelės avadinimas:"=>array("type"=>"text","value"=>input($sql['panel']),"name"=>"Panel","style"=>"width:400px"),
			"Kurioje pusėje:"=>array("type"=>"select","value"=>array("L"=>"Kairė","R"=>"Dešinė"),"selected"=>input($sql['align']),"name"=>"Align"),
			"Ar rodyti panelę?"=>array("type"=>"select","value"=>array("Y"=>"Taip","N"=>"Ne"),"selected"=>input($sql['show']),"name"=>"Show"),
			""=>array("type"=>"submit","name"=>"Redaguoti_panele","value"=>"Redaguoti")
		);
		
		require_once(HOME_DIR."variklis/class/forms_inputs.php");
		$bla = new forma();
		lentele(input($sql['file']." - ".$sql['panel']),$bla->form($panele,"Panelės redagavimas"));
	}
}

//Paneliu lygiavimas
elseif (isset($_POST['sortableListsSubmitted']) && !isset($url['n'])) {
	$orderArray = SLLists::getOrderArray($_POST['paneles_kaire'],'kaire');
	foreach($orderArray as $item) {
		$sql = $db->uzklausa("UPDATE `panel` set place=".$db->fix($item['order'])." WHERE `id`=".$db->fix($item['element']));
	}
	$orderArray = SLLists::getOrderArray($_POST['paneles_desine'],'desine');
	foreach($orderArray as $item) {
		$sql = $db->uzklausa("UPDATE `panel` set place=".$db->fix($item['order'])." WHERE `id`=".$db->fix($item['element']));
	}
	perkelti("Location: ?id,46;a,9");
}

//atvaizduojam blokus
	$sql = "SELECT id, panel, place from panel WHERE align='L' order by place";
	$recordSet = $db->uzklausa($sql);
	$listArray = array();
	while ($record = $db->masyvas($recordSet)) {
		$listArray[] = sprintf($listItemFormat,$record['id'],$record['panel'],$record['id'],$record['id']);
	}
	$sql1 = "SELECT id, panel, place from panel WHERE align='R' order by place";
	$recordSet1 = $db->uzklausa($sql1);
	$listArray1 = array();
	while ($record1 = $db->masyvas($recordSet1)) {
		$listArray1[] = sprintf($listItemFormat,$record1['id'],$record1['panel'],$record1['id'],$record1['id']);
	}
	mysql_free_result($recordSet);
	mysql_free_result($recordSet1);
	$listHTML = implode("\n",$listArray);$listHTML1 = implode("\n",$listArray1);

?>
<html>
<head>
	<?
	$sortableLists->SLLists("javascript");	//kelias iki js failu
	$sortableLists->printTopJS();				//atspauzdinam pagrindinius javascript
	?>
</head>

<body>
<?
echo "\n<div class=\"title\">Šiuo metu svetainėje rodomų panelių administravimas</div>\n";
echo "<div class=\"vidus\">";
echo "<table width=\"100%\"><tr><td valign=\"top\"><fieldset>\n<legend>Kairė</legend>\n";
printf('<ul class="sortableList" id="kaire">%s </ul>', $listHTML);
echo "<br/>";
$sortableLists->printForm('?id,46;a,9;p,l', 'POST', 'Saugoti', 'button');
$sortableLists->printBottomJS();
echo "</fieldset>\n</td>";

echo "<td valign=\"top\"><fieldset>\n<legend>Desinė</legend>\n";
printf('<ul class="sortableList" id="desine">%s </ul>', $listHTML1);
echo "<br/>";
$sortableLists->printBottomJS();
echo "<button onClick=\"window.location='?id,46;a,9;n,1';\">Naujas blokas</button>
</fieldset>
</td></tr></table></div>\n";

echo "</div>\n";
?>